import csv

def read_csv_plus(csv_name):
    f = open(csv_name, 'r')
    reader = csv.reader(f)
    result = []
    for row in reader:
        res=[]
        for re in row:
            re=re[1:-1].split(', ')
            re= [float(s) for s in re]
            res.append(re)
        result.append(res)
    return result

def read_csv(csv_name):
    f = open(csv_name, 'r')
    reader = csv.reader(f)
    result = []
    for row in reader:
        row = [float(s) for s in row]
        result.append(row)
    sorted(result, key=lambda x: x[0], reverse=False)
    return result

def redundancy_data(useless_keypoint_no,pca_result_devided):
    pca_result_devided = read_csv_plus(pca_result_devided)
    length=len(pca_result_devided)
    final_data=[]
    for  i in range(length):
        if i==useless_keypoint_no:
            continue
        else:
            for x in pca_result_devided[i]:
                data1=[]
                data1.append(x[0])
                data2=x[9:53]
                data=data1+data2
                final_data.append(data)
    list_to_csv(sorted(final_data, key=lambda x: x[0], reverse=False))
    return  sorted(final_data, key=lambda x: x[0], reverse=False)

def list_to_csv(data):
    # filename= "data.txt"
    f = open('redundancy_data_result.csv', 'w', encoding='utf-8', newline='')
    csv_writer = csv.writer(f)
    for res in data:
        csv_writer.writerow(res)
    f.close()

input_example = [42,'data_devide.csv']
output_example = 'redundancy_data_example_1.csv'
output = redundancy_data(input_example[0],input_example[1])


def compare(o_e, o):
    result_example = read_csv(o_e)
    result = o
    if (result_example == result):
        return True
    else:
        return False

print("")

# 用例目的
print("该用例目的为：测试是否正常删除正确的冗余数据")

print("")

# 子用例编号
print("子用例编号：redundancy_data_1")

print("")

print("****************************")
print("当前输入为：")
print(input_example)

print("")

print("****************************")
print("当前输出格式为:")
print([len(output),len(output[0])])
print("****************************")
print("由于数据量过大，展示输出中的前五个数据:")
# 输出处理后数据
print(output[:5])

print("")

print("****************************")
print("预设输出格式为:")
print([len(read_csv(output_example)),len(read_csv(output_example)[0])])

print("****************************")
print("由于数据量过大，展示预设输出中的前五个数据:")
# 输出处理后数据
print(read_csv(output_example)[:5])

print("")

print("****************************")
print("是否正确:")
# 输出对比结果
# 需要写一个compare函数
if compare(output_example, output):
    print("输出与预定目标相符")
else:
    print("输出与预定目标不符")
# 输出对比结果

print("\n")
